package com.cuitrip.im.model;

import android.os.Parcel;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import io.rong.common.ParcelUtils;
import io.rong.common.RLog;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

/**
 * Created by ak on 16/2/23.
 */
@MessageTag(
        value = "CT:InfoMessageIdentifier",
        flag = MessageTag.ISPERSISTED
)
public class CTInformationNotificationMessage extends MessageContent {

    private String content;
    protected String extra;
    public static final Creator<CTInformationNotificationMessage> CREATOR = new Creator() {
        public CTInformationNotificationMessage createFromParcel(Parcel source) {
            return new CTInformationNotificationMessage(source);
        }

        public CTInformationNotificationMessage[] newArray(int size) {
            return new CTInformationNotificationMessage[size];
        }
    };

    public byte[] encode() {
        JSONObject jsonObj = new JSONObject();

        try {
            if (!TextUtils.isEmpty(this.getContent())) {
                jsonObj.put("content", this.getContent());
            }

            if (!TextUtils.isEmpty(this.getExtra())) {
                jsonObj.put("extra", this.getExtra());
            }
        } catch (JSONException var4) {
            RLog.e(this, "JSONException", var4.getMessage());
        }

        try {
            return jsonObj.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException var3) {
            var3.printStackTrace();
            return null;
        }
    }

    protected CTInformationNotificationMessage() {
    }

    public static CTInformationNotificationMessage obtain(String message) {
        CTInformationNotificationMessage model = new CTInformationNotificationMessage();
        model.setContent(message);
        return model;
    }

    public CTInformationNotificationMessage(byte[] data) {
        String jsonStr = null;

        try {
            jsonStr = new String(data, "UTF-8");
        } catch (UnsupportedEncodingException var5) {
            ;
        }

        try {
            JSONObject e = new JSONObject(jsonStr);
            if (e.has("content")) {
                this.setContent(e.optString("content"));
            }

            if (e.has("extra")) {
                this.setExtra(e.optString("extra"));
            }
        } catch (JSONException var4) {
            RLog.e(this, "JSONException", var4.getMessage());
        }

    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, this.getContent());
        ParcelUtils.writeToParcel(dest, this.getExtra());
    }

    public CTInformationNotificationMessage(Parcel in) {
        this.setContent(ParcelUtils.readFromParcel(in));
        this.setExtra(ParcelUtils.readFromParcel(in));
    }

    public CTInformationNotificationMessage(String message) {
        this.setContent(message);
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getExtra() {
        return this.extra;
    }

    public void setExtra(String extra) {
        this.extra = extra;
    }
}